package client.comm.baoding.ui.vm

import android.app.Application
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import client.comm.commlib.network.data.JsonResult
import client.comm.commlib.network.data.Ret
import client.comm.commlib.out_time
import client.comm.commlib.toast.toast
import client.comm.baoding.ui.viewmodel.MiddleVm
import client.comm.baoding.api.bean.UserInfo
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import retrofit2.Response

class WdgpViewModel(val app: Application) : MiddleVm(app) {
    val call = MutableLiveData<JsonResult<Ret>>()
    val isShowing = MutableLiveData<Boolean>()
    val guadanJe = MutableLiveData<String>("")
    val huzhuanJe = MutableLiveData<String>("")
    val duifangSjh = MutableLiveData<String>("")
    val gmMyRet = MutableLiveData<JsonResult<Ret>>()

    val clickGmRet = MutableLiveData<JsonResult<Ret>>()
    val hzGmRet = MutableLiveData<JsonResult<Ret>>()

    val userInfo = MutableLiveData<UserInfo>()

    fun gmClick(it: String, pay_type: String) {
        runBlocking {
            isShowing.value = true
            flow {
                var emm: Response<JsonResult<Ret>>? = null
                out_time {
                    val ret = server.validatePayPwd(it)
                    emm = ret
                }
                emit(emm)
            }.map {
                var emm: Response<JsonResult<Ret>>? = it
                it?.body()?.let {
                    if (it.isSuccess) {
                        out_time {
                            val ret = server.trade_hang_order(guadanJe.value, "1", pay_type)
                            emm = ret
                        }
                    }
                }
                emm
            }.flowOn(Dispatchers.IO)
                .onCompletion {
                    isShowing.postValue(false)
                }.catch { cause ->
                    if (cause != null) {
//                        toast(cause.message)
                        toast("网络错误")
                    }
                }.collectLatest {
                    gmMyRet.value = it?.body()

                }
        }
    }

    fun gmaiClick(it: String) {
        if (guadanJe.value.isNullOrEmpty()) {
            toast("请输入挂卖金额")
            return
        }
        runBlocking {
            isShowing.value = true
            flow {
                var emm: Response<JsonResult<Ret>>? = null
                out_time {
                    val ret = server.validatePayPwd(it)
                    emm = ret
                }
                emit(emm)
            }.map {
                var emm: Response<JsonResult<Ret>>? = it
                it?.body()?.let {
                    if (it.isSuccess) {
                        out_time {
                            val ret = server.trade_hang_order(guadanJe.value, "2", "1")
                            emm = ret
                        }
                    }
                }
                emm
            }.flowOn(Dispatchers.IO)
                .onCompletion {
                    isShowing.postValue(false)
                }.catch { cause ->
                    if (cause != null) {
//                        toast(cause.message)
                        toast("网络错误")
                    }
                }.collectLatest {
                    gmMyRet.value = it?.body()

                }
        }
    }

    fun hz(it: String) {

        runBlocking {
            isShowing.value = true

            flow {
                var emm: Response<JsonResult<Ret>>? = null
                out_time {
                    val ret = server.validatePayPwd(it)
                    emm = ret
                }
                emit(emm)
            }.map {
                var emm: Response<JsonResult<Ret>>? = it
                it?.body()?.let {
                    if (it.isSuccess) {
                        out_time {
                            val ret = server.transfer_deal(duifangSjh.value, huzhuanJe.value)
                            emm = ret
                        }
                    }
                }
                emm
            }.flowOn(Dispatchers.IO)
                .onCompletion {
                    isShowing.postValue(false)
                }.catch { cause ->
                    if (cause != null) {
//                        toast(cause.message)
                        toast("网络错误")
                    }
                }.collectLatest {
                    hzGmRet.value = it?.body()
                }
        }

    }

    fun zmaiClick(it: String, id: String, num: String, payType: String) {
        runBlocking {
            isShowing.value = true
            flow {
                var emm: Response<JsonResult<Ret>>? = null
                out_time {
                    val ret = server.validatePayPwd(it)
                    emm = ret
                }
                emit(emm)
            }.map {
                var emm: Response<JsonResult<Ret>>? = it
                it?.body()?.let {
                    if (it.isSuccess) {
                        out_time {
                            val ret = server.trade_pick_order(id, num, payType)
                            emm = ret
                        }
                    }
                }
                emm
            }.flowOn(Dispatchers.IO)
                .onCompletion {
                    isShowing.postValue(false)
                }.catch { cause ->
                    if (cause != null) {
//                        toast(cause.message)
                        toast("网络错误")
                    }
                }.collectLatest {
                    clickGmRet.value = it?.body()
                }
        }
    }
}